package com.example.qxfw.rcyw.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.qxfw.common.entity.yj.WarnMonitorInfo;
import com.example.qxfw.rcyw.mapper.WarnMoniInfoMapper;
import com.example.qxfw.rcyw.service.WarnMoniInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WarnMoniInfoServiceImpl implements WarnMoniInfoService {

    @Autowired
    private WarnMoniInfoMapper warnMoniInfoMapper;

    @Override
    public List<WarnMonitorInfo> getListByTime(Integer elementHour) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - elementHour);
        String warningTime = sdf.format(calendar.getTime());
        Timestamp timestamp = Timestamp.valueOf(warningTime);
        List<WarnMonitorInfo> warnMoniInfos = warnMoniInfoMapper.selectWarnMoniInfo(timestamp, "制作系统");
        /*BigDecimal bigDecimal = new BigDecimal(30);
        List<WarnMonitorInfo> yjList = warnMoniInfos.stream().filter(value -> value.getMonitorvalue().compareTo(bigDecimal) > 0).collect(Collectors.toList());
        */
        List<WarnMonitorInfo> collect = warnMoniInfos.stream().filter(WarnMonitorInfo::getIsrevised).collect(Collectors.toList());
        return collect;
    }

    @Override
    public Integer updateWarnMonitor(String ids) {
        int count = 0;
        String[] idarr = ids.split(",");
        for (int k = 0; k < idarr.length; k++) {
            WarnMonitorInfo info = new WarnMonitorInfo();
            info.setIsremoved((short) 1);
            LambdaUpdateWrapper<WarnMonitorInfo> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(WarnMonitorInfo::getId, Integer.parseInt(idarr[k]));
            count += warnMoniInfoMapper.update(info, updateWrapper);
        }
        return count;
    }
}
